package sort;

/**
 * 荷兰国旗问题：小于num的排左边，等于的放中间，大于的放右边
 */
public class DutchFlag {

    public static void main(String[] args) {
        int[] arr = {5,7,3,2,5,9,2,1,0,4};
        DutchFlagSort(arr,5);
    }
    public static void DutchFlagSort(int[] arr,int num){
        int left = 0;
        int right = arr.length-1;
        for (int i = 0; i <= right;i++) {
            if(arr[i] < num){
                swap(arr,i,left++);
            }else if(arr[i] > num){
                swap(arr,i,right--);
                i--;
            }
        }
    }
    public static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
